import java.util.*;
import java.io.*;


public class GenerateDirectoryList {

    private static String srcDir     ="C:/myProjects/apache2/htdocs";
    private static String sevDir     ="C:/myServers/ApacheGroup/Apache2/htdocs";
    private static String sevName    ="3WCenter";
    private static String distDir    ="C:/myServers/ApacheGroup/Apache2/htdocs";
    private static String fileName   ="index.html";
    private static String options    ="profile";

    public static void main( String args[] )
    {
	GenerateDirectoryList maker = new GenerateDirectoryList();

	if(args.length != 5) {
	    System.out.println("==============================================================================================");
	    System.out.println("[Source Directory][Server Home][Server Name][Output path][file name][Options(Profile,Full)]");
	    System.out.println("==============================================================================================");
	    System.exit(0);
	}

	/*
	    if (currentDir.indexOf('/') != -1) {
		pdir_id= currentDir.substring(0, currentDir.indexOf('/'));
		dir_id = currentDir.substring(currentDir.indexOf('/')+1);
	    }
	    else {
		pdir_id = "3wcenter";
		dir_id  = currentDir;
	    }
	}
	*/

	maker.setInputs(args[0],args[1],args[2],args[3],args[4]);
	maker.generateHtmlFile();
    }

    public void setInputs (String src_dir, String sev_name, String dist_dir, String f_name, String pro_full) {

	File f ;
	f = new File (src_dir);
	if (!f.isDirectory()) {
	    System.out.println("Source Directory not found:"+src_dir);
	    System.exit(0);
	}

	srcDir = src_dir;
	sevName = sev_name;

	f = new File (dist_dir);
	if (!f.isDirectory()) {
	    f.mkdirs();
	}
	distDir = dist_dir;

	f = new File (dist_dir, f_name);
	if (!f.isFile()) {
	    f.delete();
	}

	fileName = f_name;

	if(pro_full.equals("profile") || pro_full.equals("full")) {
	    options = pro_full;
	}
	else {
	    System.out.println("No such option:"+pro_full);
	    System.exit(0);
	}
    }

 
    private void generateHtmlFile()
    {

	try {
	    RandomAccessFile outputFile = new RandomAccessFile( (distDir+"/"+fileName), "rw" );
	    addHtmlHeadToLeftBar(outputFile);
	    addHtmlEnd(outputFile);
	    closeFile(outputFile);
	}
	catch ( IOException e ) {
	}
    }
    /*
    private void loadLeftBarProfile() {

	String file_dir = null;
	if(currentDir.equals("/")) {
	    file_dir = srcDir;
	}
	else {
	    file_dir = srcDir+"/"+currentDir;
	}

	File check_profile = new File (file_dir, "_profile.txt");

	if (check_profile.exists()) {
	    ProfileLoader loader = new ProfileLoader();
	    loader.setFileName(file_dir+"/_profile.txt");
	    loader.loadProfile();
	    proVector = loader.getVector();
	}
	else {
	    System.out.println("No profile for this directory");
	}

    }
    */
    public void addHtmlHeadToLeftBar(RandomAccessFile outputFile)
    {
	String dir_id[] = null;
	String c_dir =  null;
	String temp = null;
	int i = 0;

	try {
	    c_dir = distDir.replaceAll(sevDir,"");
	    StringTokenizer tokens = new StringTokenizer(c_dir,"\\");

	    while (tokens.hasMoreTokens()) {
		temp = (String)tokens.nextToken();
		if (!temp.startsWith("_") && (temp != null)) {
		    dir_id[i] = temp;
		    i++;
		}
		if (temp.startsWith("_")) {
		    break;
		}
	    }

	    outputFile.writeBytes("<HTML><HEAD><TITLE>Direcotry:["+c_dir+"]["+fileName+"]</TITLE></HEAD>");
	    outputFile.writeBytes("<BODY>");

	    outputFile.writeBytes("<TABLE border='0' width='300' height='100'>");
	    outputFile.writeBytes("<TR><TD><A href=/3wcg.html><IMG height ='100' width='100' border=0 SRC=/_system/images/3wc.JPG></A>");
	    outputFile.writeBytes("</TD></TR></TABLE><TABLE>");
	    
	    /*
	      if(pdir_id.equals("3wcenter")) {
	      if (dir_id!=null) {
	      //display 3wcenter link
	      outputFile.writeBytes("<tr><td><A href=/_system/html/index_left.html>");
	      outputFile.writeBytes(pdir_id.toUpperCase()+"</A></TD></TR>");
	      //display this directory
	      outputFile.writeBytes("<tr><td><IMG src=/_system/images/L.gif>");
	      outputFile.writeBytes(dir_id.toUpperCase()+"</TD></TR>");
	      }
	      }
	      else {
	      //display 3wcenter link
	      outputFile.writeBytes("<TABLE width=300><TR><TD><a href=/_system/html/index_left.html>");
	      outputFile.writeBytes("3WCENTER</a></TD></TR>");
	      //display parent link
	      outputFile.writeBytes("<tr><td><A href=/"+pdir_id+"><img src=/_system/images/L1.gif border=0>");
	      outputFile.writeBytes(pdir_id.toUpperCase()+"</A></TD></TR>");
	      //display this directory
	      outputFile.writeBytes("<tr><td><IMG src=/_system/images/L2.gif border=0>");
	      outputFile.writeBytes(dir_id.toUpperCase()+"</TD></TR>");
	      }
	    */
	    outputFile.writeBytes("<TR><TD><a href=/index/servlet/SideBarEditServlet target=mainFrame>");
	    outputFile.writeBytes("<img border=0 src=/_system/images/edit.gif></a></TD></TR>");
	    outputFile.writeBytes("</TABLE>");
	}
	catch ( IOException io ) {
	}
    }

/*
    public void addHtmlProfileToLeftBar()
    {

	Vector proVector = new Vector();

	String    url_name   = null;
	String    url_adds   = null;
	int       i          = 0;
	try {
	    Enumeration enum = proVector.elements();  
	    while (enum.hasMoreElements()) {
		url_name = (String)enum.nextElement();
		url_adds = (String)enum.nextElement();
		outputFile.writeBytes("\n<A href ="+url_adds+">"+url_name.toUpperCase()+"</A><BR>\n");
		i++;
	    }
	}
	catch ( IOException io ) {
	}
    }
	
    public void addHtmlBodyToLeftBar()
    {
	
	String   sub_dir       = null;
	String[] file_list     = null;

	file_list = getDirList(baseDir);

	for (int n = 0; n < file_list.length; n++) {

	    File f_file_list = new File (baseDir, file_list[n]);
	    
	    if (f_file_list.isDirectory()) {
		sub_dir = file_list[n];
		System.out.println("subDir="+sub_dir);

		if(!sub_dir.startsWith("_")) {

				    try {
			outputFile.writeBytes("<A href=/index/servlet/SideBarServlet?pdir_id="+
					      currentDir+"&dir_id="+sub_dir+">");
			outputFile.writeBytes(sub_dir+"</A><BR>");
		    }
		    catch ( IOException io ) {
		    }
		}
	    }
	}
    }
*/
    public void addHtmlEnd(RandomAccessFile outputFile )
    {
	try {
	    outputFile.writeBytes("\n</BODY></HTML>");
	}
	catch ( IOException io ) {
	}
    }

    private void closeFile(RandomAccessFile raf) 
    {
	try {
	    raf.close();
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

    private void makeDirectory(String dir_name) {
	File f = new File (dir_name);
	if (!f.isDirectory()) {
	    f.mkdirs();
	}
    }
}



